# Create your views here.
from django.http import HttpResponseRedirect, HttpResponse
from django.template import Context, loader, RequestContext
from django.http import Http404
from django.shortcuts import get_object_or_404, render_to_response
from django.core.urlresolvers import reverse
import functions as f

def index(request):
    if 'GuessNum_digNum' in request.session:
        GuessNum_digNum = request.session['GuessNum_digNum']
    else:
        GuessNum_digNum = 0
    return render_to_response('GuessNum/index.html', {'GuessNum_digNum': GuessNum_digNum},context_instance=RequestContext(request))

def results(request):
    if 'GuessNum_digNum' in request.session:
        digNum = int(request.session['GuessNum_digNum'])
    else:
        digNum = int(request.POST['digNum'])
    Ans = request.session.get('GuessNum_Ans', f.genAns(digNum))
    request.session['GuessNum_Ans'] = Ans
    request.session['GuessNum_digNum'] = digNum    
    uGuess = request.POST['uGuess']
    (nA,nB) = f.compNumber(Ans, uGuess, digNum)
    if nA == digNum:
        request.session.clear()
    return render_to_response('GuessNum/results.html', {'Ans':Ans, 'nA': nA, 'nB':nB})
